<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Ups extends CI_Controller {
	private $baseconfig;
	public function __construct(){
		parent::__construct();
		$this->load->library('ComUploads');
		$this->load->library('ComImages');
		$this->up=new ComUploads();
		$this->img=new ComImages();
		$this->baseconfig=sys_config('base');
	}
	public function return_js($data){
		echo $data;exit;
		//跨域直接返回 ie 不支持
		//$str = '<script type="text/javascript">parent.postMessage('.$data.', "*")</script>';
		//echo $str;exit;
		//返回原网站 在返回。所有支持。不跨越
		//$str = '<script type="text/javascript">window.parent.upfile('.$return.')</script>';
		//echo $str;
	}
	//图片单张上传
	public function imageone(){
		if(!isset($_POST['savePath'])){
			ajax_return(0,'savePath 参数不存在');
		}
		//获取配置信息
		$extsstr=$this->baseconfig['exts'];
		$exts=explode(',',$extsstr);
		$setconfig[]=array('exts',$exts);
		$setconfig[]=array('maxSize',$this->baseconfig['maxsize']);
		$savePath=$_POST['savePath'];
		$setconfig[]=array('savePath',$savePath);
		$this->up->setconfig($setconfig);
		$files_arr=$_FILES;
		$files_arr=array_values($files_arr);
		$files=$files_arr[0];
		$info=$this->up->upload_one($files);
		//更新键值为0
		$info=array_values($info);
		//返回回调函数
		$return=array();
		if(!$info){
			$return['status']=false;
			$return['message']=$this->up->getError()[0];
		}else{
			$return['status']=true;
			$return['message']="上传成功";
			$return['imgshow']=PMSROOT.ltrim($info[0]['filename'],'.');
			$return['img']=ltrim($info[0]['filename'],'.');
			$return['name']=$info[0]['name'];
		}
		$return=json_encode($return);
		$this->return_js($return);
	}
	//文章上传
	public function attachmentmore(){
		$return=array();
		if(!isset($_POST['savePath'])){
			ajax_return(0,'savePath 参数不存在');
		}
		$savePath=$_POST['savePath'];
		$setconfig[]=array('savePath',$savePath);
		$setconfig[]=array('exts',array('jpg','jpeg','png','gif','docx','xlsx','doc','xls','pptx','ppt'));
		$this->up->setconfig($setconfig);
		$files=$_FILES;
		$files=array_values($files);
		$files=$files[0];
		$info=$this->up->upload_more($files);
		//获取上传图片错误信息
		$returnerrinfo=array();
		$returnerrinfo=$this->up->getError();
		if(!empty($info)){//上传成功的图片不能为空
			foreach($info as $k=>$v){
				$v['imgshow']=PMSROOT.ltrim($v['filename'],'.');//返回使用的图片地址
				$v['img']=trim($v['filename'],'.');
				$v['fileshowname']=basename($v['filename']);
				$info[$k]=$v;
			}
		}
		if(!empty($returnerrinfo)){
			$errinfo=implode('<br />',$returnerrinfo);
		}else{
			$errinfo=0;
		}
		$info=array_values($info);
		if(empty($info)){
			$return['info']=0;
		}else{
			foreach($info as $k=>$v){
				$v['imgshow']=trim($v['imgshow'],'.');
				$info[$k]=$v;
			}
		}
		$return['info']=$info;
		$return['errinfo']=$errinfo;
		$return2=json_encode($return);
		$this->return_js($return2);
	}
}